package com.co.vote.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.co.common.utils.MD5;
import com.co.server.entity.user.TUsers;
import com.co.server.service.user.TUsersService;
import com.co.vote.constant.HttpStatusCode;
import com.co.vote.utils.SessionUtil;

/**
 * 用户页面
 * @author ZYL
 * @date 2016年11月27日 下午10:37:21
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	TUsersService tUsersService;
	/**
	 * 用户中心主页面
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/center", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session) {
		Integer userId = SessionUtil.getUserId(session);
		TUsers user = tUsersService.queryById(userId);
		model.addAttribute("user", user);
		return "/user/center";
	}
	//编辑用户信息
	@RequestMapping(value = "/editUserInfo", method = { RequestMethod.GET, RequestMethod.POST })
	public String editUserInfo( Model model,HttpSession session) {
		Integer userId = SessionUtil.getUserId(session);
		TUsers user = tUsersService.queryById(userId);
		model.addAttribute("user", user);
		return "/user/editUserInfo";
	}
	//保存用户信息
	@RequestMapping(value = "/saveUserInfo", method = { RequestMethod.GET, RequestMethod.POST })
	public String saveUserInfo( Model model,HttpSession session,TUsers user) {
		tUsersService.updateByIdSelective(user);
		TUsers userTmp = tUsersService.queryById(user.getUserId());
		SessionUtil.setUser(userTmp, session);
		return "redirect:/user/center";
	}
	
	//修改密码页
	@RequestMapping(value = "/editUserPwd", method = { RequestMethod.GET, RequestMethod.POST })
	public String editUserPwd( Model model,HttpSession session) {
		return "/user/editUserPwd";
	}
	
	//保存新密码 
	@RequestMapping(value = "/saveUserPwd", method = { RequestMethod.GET, RequestMethod.POST })
	public String saveUserPwd( Model model,HttpSession session,TUsers user) {
		Integer userId = SessionUtil.getUserId(session);
		user.setUserId(userId);
		String passwordMd5 = MD5.encodeStr(user.getUserPassword());
		user.setUserPassword(passwordMd5);
		tUsersService.updateByIdSelective(user);
		return "redirect:/user/center";
	}
	
	//检查原密码是否正确
	@ResponseBody
	@RequestMapping(value = "/checkOldPwd", method = { RequestMethod.GET, RequestMethod.POST })
	public Map<String,Object> checkOldPwd( Model model,HttpSession session,String password) {
		Map<String,Object> mapResult = new HashMap<String,Object>();
		Integer userId = SessionUtil.getUserId(session);
		TUsers tUsers = tUsersService.queryById(userId);
		String passwordMd5 = MD5.encodeStr(password);
		if (tUsers == null || !tUsers.getUserPassword().equals(passwordMd5)) {
			mapResult.put("code", HttpStatusCode.CODE_ERROR);
			mapResult.put("desc", "原密码不正确");
			return mapResult;
		}
		mapResult.put("code", HttpStatusCode.CODE_SUCCESS);
		return mapResult;
	}
	
	
	
	
}
	
